//
//  GYOrderCell.m
//  GoodLuck
//
//  Created by apple on 2018/3/22.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYOrderCell.h"
#import "GYOrderStore.h"
#import "GYOrderGooodsCell.h"
#import "GYNetWorkTools.h"
#import "XLPasswordView.h"
@interface GYOrderCell () <UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate,XLPasswordViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *storeNameLable;
@property (weak, nonatomic) IBOutlet UILabel *isPaylable;
@property (weak, nonatomic) IBOutlet UILabel *goodsFreigtLable;

@property (weak, nonatomic) IBOutlet UILabel *goodsSumPriceLable;
@property (weak, nonatomic) IBOutlet UILabel *goodsNumDesLable;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightCons;

/** 数据源 */
@property (nonatomic, strong) NSArray *orderGoodsList;

@property (weak, nonatomic) IBOutlet UIButton *delBtu;
@property (weak, nonatomic) IBOutlet UIImageView *delImageView;
@property (weak, nonatomic) IBOutlet UILabel *delLable;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtu;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtu;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *confirmViewHeightCons;

@property (weak, nonatomic) IBOutlet UIView *conformView;

@end
@implementation GYOrderCell


- (IBAction)onClick:(UIButton *)sender {
    if (self.orderModel.isIf_lock) {
        UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"该订单退款正在审核中,不能重复操作!" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }else {
        switch (sender.tag) {
            case 1:
            {
                [GYNetWorkTools requestDeleteOrder:self.orderModel.order_id success:^(NSDictionary *obj) {
                    int code = [[obj objectForKey:@"code"] intValue];
                    if (code == 200) {
                        [EasyTextView showSuccessText:@"删除订单成功!"];
                        self.deleteOrderCallBack();
                    }else if (code == 400) {
                        NSLog(@"删除订单失败...");
                        [EasyTextView showText:@"删除订单失败!"];
                    }
                } error:^(NSError *error) {
                    NSLog(@"DeleteOrderFailed.....====%@",error);
                    [EasyTextView showText:@"删除订单失败!"];
                }];
            }
                break;
                
            case 2:
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"确定取消订单?" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alertView show];
            }
                break;
                
            case 3:
            {
                if ([self.orderModel.order_state intValue] == 30) {
                    //这里要收货
                    //这里要检查用户有没有设置应用内支付密码
                    //判断用户有没有设置支付密码 (不需要支付现金)
                    [GYNetWorkTools requestVerPayPwd:^(NSDictionary *obj) {
                        //NSLog(@"obj========%@",obj);
                        int code = [[obj objectForKey:@"code"] intValue];
                        if (code == 200) {
                            int datas = [[obj objectForKey:@"datas"] intValue];
                            if (datas == 1) {
                                [self setupPayPwdView];
                            }else {
                                [EasyTextView showText:@"您还没有设置支付密码!"];
                            }
                        }else if (code == 400) {
                            [EasyTextView showText:@"您还没有设置支付密码!"];
                        }
                    } error:^(NSError *error) {
                        [EasyTextView showText:@"您的网络可能不太通畅..."];
                        NSLog(@"error=======%@",error);
                    }];
                    
                }else if ([self.orderModel.order_state intValue] == 40) {
                    self.confirmOrEvaluateCallBack(40);
                }
            }
                break;
                
            default:
                break;
        }
    }
}

/**
 初始化支付密码
 */
- (void)setupPayPwdView {
    XLPasswordView *passwordView = [XLPasswordView passwordViewWithKeyboardType:XLPasswordViewKeyboardTypeSystem];
    passwordView.delegate = self;
    [passwordView showPasswordInView:[UIApplication sharedApplication].keyWindow];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.tableView.rowHeight = 80;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.cancelBtu.layer.cornerRadius = 4;
    self.cancelBtu.layer.borderColor = RGBColor(201, 201, 201).CGColor;
    self.cancelBtu.layer.borderWidth = 1;
    self.confirmBtu.layer.cornerRadius = 4;
    self.tableView.bounces = NO;
    self.tableView.userInteractionEnabled = NO;
}

- (void)setOrderModel:(GYOrder *)orderModel {
    _orderModel = orderModel;
    
    switch ([orderModel.order_state intValue]) {
        case 0:
        {
            self.isPaylable.text = @"已取消";
            self.delBtu.hidden = NO;
            self.delImageView.hidden = NO;
            self.delLable.hidden = NO;
            self.cancelBtu.hidden = YES;
            
            //设置底部视图高度约束为0;
            self.confirmViewHeightCons.constant = 0;

        }
            break;
            
        case 10:
        {
            self.isPaylable.text = @"待付款";
            self.delBtu.hidden = YES;
            self.delImageView.hidden = YES;
            self.delLable.hidden = YES;
            self.cancelBtu.hidden = NO;
            
            //设置底部视图高度约束为0;
            self.confirmViewHeightCons.constant = 0;
        }
            break;
            
        case 20:
        {
            self.isPaylable.text = @"待发货";
            self.delBtu.hidden = YES;
            self.delImageView.hidden = YES;
            self.delLable.hidden = YES;
            self.cancelBtu.hidden = YES;
            
            //设置底部视图高度约束为0;
            self.confirmViewHeightCons.constant = 0;
        }
            break;
            
        case 30:
        {
            self.isPaylable.text = @"待收货";
            self.delBtu.hidden = YES;
            self.delImageView.hidden = YES;
            self.delLable.hidden = YES;
            self.cancelBtu.hidden = YES;
            
            //设置底部视图高度约束为0;
            self.confirmViewHeightCons.constant = 54;
            [self.confirmBtu setTitle:@"确认收货" forState:UIControlStateNormal];
        }
            break;
            
        case 40:
        {
            self.isPaylable.text = @"待评价";
            self.delBtu.hidden = YES;
            self.delImageView.hidden = YES;
            self.delLable.hidden = YES;
            self.cancelBtu.hidden = YES;
            
            //设置底部视图高度约束为0;
            self.confirmViewHeightCons.constant = 54;
            [self.confirmBtu setTitle:@"评价一下" forState:UIControlStateNormal];

        }
            break;
            
        default:
            break;
    }
    
    //赋值
    GYOrderStore *store = orderModel.extend_store;
    self.storeNameLable.text = store.store_name;
    
    self.orderGoodsList = orderModel.extend_order_goods;
    self.tableViewHeightCons.constant = orderModel.extend_order_goods.count * 80;
    [self.tableView reloadData];

    self.goodsFreigtLable.text = [NSString stringWithFormat:@"含运费%@",orderModel.shipping_fee];
    self.goodsSumPriceLable.text = [NSString stringWithFormat:@"¥%@",orderModel.order_amount];
    self.goodsNumDesLable.text = [NSString stringWithFormat:@"共%zd件商品,合计",orderModel.extend_order_goods.count];
    
}

- (CGFloat)getHeight:(GYOrder *)model {
    self.orderModel = model;
    
    [self layoutIfNeeded];
    
    return CGRectGetMaxY(self.conformView.frame);
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.orderGoodsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYOrderGooodsCell *cell = [GYOrderGooodsCell cellWithTableView:tableView];
    cell.goods = self.orderGoodsList[indexPath.row];
    
    return cell;
}


#pragma mark -- UIAlrtViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [GYNetWorkTools requestCancelOrder:self.orderModel.order_id success:^(NSDictionary *obj) {
            int code = [[obj objectForKey:@"code"] intValue];
            if (code == 200) {
                [EasyTextView showSuccessText:@"取消订单成功!"];
                self.cancelOrderCallBack();
            }else if (code == 400) {
                [EasyTextView showText:@"取消订单失败!"];
            }
        } error:^(NSError *error) {
            [EasyTextView showText:@"取消订单失败!"];
            NSLog(@"CancelOrderFailed===%@",error);
        }];
    }
}

#pragma mark -- XLPasswordViewDelegate
/**
 *  输入密码位数已满时调用
 */
- (void)passwordView:(XLPasswordView *)passwordView didFinishInput:(NSString *)password
{
    NSLog(@"输入密码位数已满,在这里做一些事情,例如自动校验密码");
    [self endEditing:YES];
    //验证输密码正不正确
    [GYNetWorkTools requestcheckPassword:password success:^(NSDictionary *obj) {
        //NSLog(@"obj=====%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        if (code == 200) {
            [passwordView removeFromSuperview];
            //确认收货
            [GYNetWorkTools requestConfirmationReceipt:self.orderModel.order_id success:^(NSDictionary *obj) {
                int code = [[obj objectForKey:@"code"] intValue];
                if (code == 200) {
                    [EasyTextView showSuccessText:@"确认收货成功!"];
                    self.confirmOrEvaluateCallBack(30);
                }else if (code == 400) {
                    NSDictionary *datas = [obj objectForKey:@"datas"];
                    [EasyTextView showText:@"确认收货失败!"];
                    NSLog(@"确认收货失败...==%@",[datas objectForKey:@"error"]);
                }
            } error:^(NSError *error) {
                [EasyTextView showText:@"确认收货失败!"];
                NSLog(@"ConfirmationReceiptFailed====%@",error);
            }];
            
        }else {
            [EasyTextView showText:@"您输入的密码不正确!"];
        }
    } error:^(NSError *error) {
        [EasyTextView showText:@"您的网络可能不太通畅!"];
        NSLog(@"error====%@",error);
    }];
}

/**
 *  用户输入密码时调用
 *
 *  @param passwordView 视图
 *  @param password     输入的密码文本
 */
- (void)passwordView:(XLPasswordView *)passwordView passwordTextDidChange:(NSString *)password
{
    NSLog(@"%@",password);
}

/**
 *  点击了忘记密码时调用
 */
- (void)passwordViewClickForgetPassword:(XLPasswordView *)passwordView
{
    NSLog(@"点击了忘记密码,在这里做一些事情");
}
@end
